package parkshop.pmcourse.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import parkshop.pmcourse.domain.Customer;
import parkshop.pmcourse.domain.Shop;
import parkshop.pmcourse.domain.ShopOwner;
import parkshop.pmcourse.dto.User;
import parkshop.pmcourse.service.CustomerService;
import parkshop.pmcourse.service.MallAdministratorService;
import parkshop.pmcourse.service.SearchService;
import parkshop.pmcourse.service.ShopOwnerService;
import parkshop.pmcourse.service.ShopService;
import parkshop.pmcourse.utils.ConstantUtil;

/*
 * 管理员
 * 进入此类中的 url 都是被拦截器验证已经登陆了的。
 */
@Controller
@RequestMapping("/administrator")
public class MallAdministratorCtrl {
	
	@Autowired
	private MallAdministratorService mallAdministratorService;
	@Autowired
	private ShopOwnerService shopOwnerService;
	@Autowired
	private CustomerService CustomerService;
	@Autowired
	private SearchService searchService;

	/*
	 * Manage Shop Owner ( 1 Shop Owner only 1 Shop)
	Approve OR Reject New Shop Owner request
	Search Shop Owner or Shop
	Blacklist(黑名单) Shop Owner(Suspended) or Shop
	Delete Shop Owner or Shop
	 */
	/*
	 *  get the number of pageNumber of shop owners info
	 *  请求第 pageNumber 页卖家的信息
	 *  
	 *  对卖家，买家的拉黑和删除管理简化为同一页面，
	 *  只显示id 和名字，需要传递四个参数即可。
	 *  
	 *  商店管理只是多些信息。但与买家，卖家管理分开。
	 *  
	 *  对于买家，卖家，商店 有 ： 分页显示，拉黑和删除
	 */
	@GetMapping("/shopOwner/list/{pageNumber}")
	public String listShopOwner(@PathVariable int pageNumber,HttpServletRequest request, Model model){
		
		List<ShopOwner> list=mallAdministratorService.getShopOwnerList(pageNumber);
		model.addAttribute("type", ConstantUtil.userTypeShopOwner);// 表示卖家类型。
		model.addAttribute("current",pageNumber);// 需要显示第几页信息
		model.addAttribute("pageNum",mallAdministratorService.getSumPagesOfShopOwnerList());// 当前数据库中买家数量总共可以分为多少页
		model.addAttribute("customerOrOwnerList", list);// 第pageNumber 页的买家信息列表
		return "mallAdministrator/customerOrOwnerManage";
	}
	// add or reject new Shop Owner request
	 @PostMapping("/openShop")
	public String openNewShop(HttpServletRequest request, Model model){
		
		return null;
	}
	 // search shop owner
	 @GetMapping("/search/shopOwner")
	 public String searchShopOwner(String name,HttpServletRequest request, Model model){
		 ShopOwner shopOwner=searchService.searchShopOwnerByName(name);
		 if(shopOwner==null)
			 return "redirect:/administrator/shopOwner/list/1";	//用户名唯一
		 	List<ShopOwner> list=new ArrayList<ShopOwner>();
		 	list.add(shopOwner);
			model.addAttribute("type", ConstantUtil.userTypeShopOwner);// 表示卖家类型。
			model.addAttribute("current",1);// 需要显示第几页信息
			model.addAttribute("pageNum",1);// 当前数据库中买家数量总共可以分为多少页
			model.addAttribute("customerOrOwnerList", list);// 第pageNumber 页的买家信息列表
			return "mallAdministrator/customerOrOwnerManage";
	 }
	 @GetMapping("/search/customer")
	 public String searchCustomer(String name,HttpServletRequest request, Model model){
		 	Customer customer=searchService.searchCustomerByName(name);
		 	if(customer==null)
		 		 return "redirect:/administrator/customer/list/1";
		 	List<Customer> list=new ArrayList<Customer>();
		 	list.add(customer);
		 	model.addAttribute("type", ConstantUtil.userTypeCustomer);
			model.addAttribute("current",1);
			model.addAttribute("pageNum",1);
			model.addAttribute("customerOrOwnerList", list);
			return "mallAdministrator/customerOrOwnerManage";
	 }
	 @GetMapping("/search/shop")
	 public String searchShop(String name,HttpServletRequest request, Model model){
		 List<Shop> list=searchService.searchShopByName(name);
		 if(list==null)
			 return "redirect:/administrator/shop/list/1";
		 model.addAttribute("current",1);
		 model.addAttribute("pageNum",list.size()%10==10?list.size()/10:list.size()/10+1);
		 model.addAttribute("shopList", list);
		 return "mallAdministrator/shopManage";
	 }
	 
	 // Blacklist(黑名单) Shop Owner(Suspended) or Shop
	 @GetMapping("/shopOwner/blacklist/{sellerId}")
	 public String blacklistShopOwner(@PathVariable int sellerId,HttpServletRequest request, Model model){
		boolean flag= mallAdministratorService.blacklistShopOwner(sellerId);
		String message=flag?"blacklist shop owner successful":"blacklist shop owner failed";
		model.addAttribute("message", message);
		 return "redirect:/administrator/shopOwner/list/1";
	 }
	 @GetMapping("/shopOwner/normalize/{sellerId}")
	 public String normalizeShopOwner(@PathVariable int sellerId,HttpServletRequest request, Model model){
		 boolean flag= mallAdministratorService.normalizeShopOwner(sellerId);
		 String message=flag?"normalize shop owner successful":"normalize shop owner failed";
		 model.addAttribute("message", message);
		 return "redirect:/administrator/shopOwner/list/1";
	 }
	 //Delete Shop Owner or Shop
	 @GetMapping("/shopOwner/delete/{sellerId}")
	 public String deleteSeller(@PathVariable int sellerId,HttpServletRequest request, Model model){
		 boolean flag=mallAdministratorService.deleteShopOwner(sellerId);
		 String message=flag?"delete shop owner successful":"delete shop owner failed";
		 model.addAttribute("message", message);
		 return "redirect:/administrator/shopOwner/list/1";
	 }
	 
	 /*
	  * Manage shop
		Search shop
		Blacklist(黑名单) shop (Suspended)
		Delete shop
	  */
		@GetMapping("/shop/list/{pageNumber}")
		public String listShop(@PathVariable int pageNumber,HttpServletRequest request, Model model){
			List<Shop> list=mallAdministratorService.getShopList(pageNumber);
			model.addAttribute("current",pageNumber);
			model.addAttribute("pageNum",mallAdministratorService.getSumPagesOfShopList());
			model.addAttribute("shopList", list);
			return "mallAdministrator/shopManage";
		}
		 // Blacklist(黑名单)	shop(Suspended) 
		 @GetMapping("/shop/blacklist/{shopId}")
		 public String blacklistShop(@PathVariable int shopId,HttpServletRequest request, Model model){
			boolean flag=mallAdministratorService.blacklistShop(shopId);
			String message=flag?"blacklist shop  successful":"blacklist shop failed";
			model.addAttribute("message", message);
			 return "redirect:/administrator/shop/list/1";
		 }
		 @GetMapping("/shop/normalize/{shopId}")
		 public String normalizeShop(@PathVariable int shopId,HttpServletRequest request, Model model){
			 boolean flag=mallAdministratorService.normalizeShop(shopId);
			 String message=flag?"blacklist shop  successful":"blacklist shop failed";
			 model.addAttribute("message", message);
			 return "redirect:/administrator/shop/list/1";
		 }
		 //Delete Shop
		 @GetMapping("/shop/delete/{shopId}")
		 public String deleteShop(@PathVariable int shopId,HttpServletRequest request, Model model){
			 boolean flag=mallAdministratorService.deleteShop(shopId);
			 String message=flag?"delete shop successful":"delete shop failed";
			model.addAttribute("message", message);
			 return "redirect:/administrator/shop/list/1";
		 }
		 @GetMapping("/shop/open/list/{pageNumber}")// 需要接受一个复选框传递过来的商店 id 集合 和 提交按钮的值（判定允许或拒绝），
		 public String listOpenShops(@PathVariable int pageNumber,HttpServletRequest request, Model model){
			
		    model.addAttribute("pageNum",mallAdministratorService.getSumOfOpenShopList());// 总计有多少份开店申请
			model.addAttribute("current",pageNumber);// 当前显示第几页开店申请
			model.addAttribute("shopList", mallAdministratorService.getOpenShopList(pageNumber));
			 return "mallAdministrator/openShopManage";
		 }
		 @GetMapping("/shop/open/allow/{id}")// 需要接受一个复选框传递过来的商店 id 集合 和 提交按钮的值（判定允许或拒绝），
		 public String allowOpenShop(@PathVariable int id,HttpServletRequest request, Model model){
			 boolean flag=mallAdministratorService.allowNewShop(id);
			 model.addAttribute("operateInfo", flag?"allow new shop successful":"allow new shop failed");
			 return "redirect:/administrator/shop/open/list/1";
		 }
		 @GetMapping("/shop/open/reject/{id}")// 需要接受一个复选框传递过来的商店 id 集合 和 提交按钮的值（判定允许或拒绝），
		 public String rejectOpenShop(@PathVariable int id,HttpServletRequest request, Model model){
			 boolean flag=mallAdministratorService.rejectNewShop(id);
			 model.addAttribute("operateInfo", flag?"reject new shop successful":"reject new shop failed");
			 return "redirect:/administrator/shop/open/list/1";
		 }
		 
	 /*
	  * Manage Customer
		Search Customer 
		Blacklist(黑名单) Customer (Suspended)
		Delete Customer
	  */
		@GetMapping("/customer/list/{pageNumber}")
		public String listCustomer(@PathVariable int pageNumber,HttpServletRequest request, Model model){
			
			List<Customer> list=mallAdministratorService.getCustomerList(pageNumber);
			model.addAttribute("type", ConstantUtil.userTypeCustomer);// 表示卖家类型。
			model.addAttribute("current",pageNumber);// 需要显示第几页信息
			model.addAttribute("pageNum",mallAdministratorService.getSumPagesOfCustomerList());// 当前数据库中买家数量总共可以分为多少页
			model.addAttribute("customerOrOwnerList", list);// 第pageNumber 页的买家信息列表
			return "mallAdministrator/customerOrOwnerManage";
		}
		 // Blacklist(黑名单)	customer(Suspended) 
		 @GetMapping("/customer/blacklist/{customerId}")
		 public String blacklistCustomer(@PathVariable int customerId,HttpServletRequest request, Model model){
			boolean flag=mallAdministratorService.blacklistCustomer(customerId);
			String message=flag?"blacklist customer successful":"blacklist customer failed";
			model.addAttribute("message", message);
			 return "redirect:/administrator/customer/list/1";
		 }
		 @GetMapping("/customer/normalize/{customerId}")
		 public String normalizeCustomer(@PathVariable int customerId,HttpServletRequest request, Model model){
			 boolean flag=mallAdministratorService.normalizeCustomer(customerId);
			 String message=flag?"blacklist customer successful":"blacklist customer failed";
			 model.addAttribute("message", message);
			 return "redirect:/administrator/customer/list/1";
		 }
		 //Delete Shop Owner or Shop
		 @GetMapping("/customer/delete/{customerId}")
		 public String deleteCustomer(@PathVariable int customerId,HttpServletRequest request, Model model){
			 boolean flag=mallAdministratorService.deleteCustomer(customerId);
			 String message=flag?"delete customer successful":"delete customer failed";
			model.addAttribute("message", message);
			 return "redirect:/administrator/customer/list/1";
		 }
	
		 /*
		  * administrator self info manage
		  */
		 @GetMapping("/personInfo")
		 public String goPersonInfo(){
			 
			 return "module/personInfo";
		 }
		 	
			@PostMapping("/personInfo/update")
			public String updatePersonInfo(User user,HttpServletRequest request, Model model){
				 User curUser=(User) request.getSession(false).getAttribute(ConstantUtil.nameOfCurUser);
				 if(user.getName()!=null&&!"".equals(user.getName().trim())){
				 curUser.setName(user.getName());
				 mallAdministratorService.updatePersonInfo(curUser);	
				 }else{
					 model.addAttribute("message", "your name can't be 0 character");
				 }
				return "module/personInfo";        	
			}
			@PostMapping("/password/update")
			public String updatePassword(String oldPassword,String newPassword,HttpServletRequest request, Model model){
				 User curUser=(User) request.getSession(false).getAttribute(ConstantUtil.nameOfCurUser);
				 String infoUpdateMessage="update person info failed,the oldPassword was not right";
				 if(oldPassword.equals(curUser.getPassword())){
				 curUser.setPassword(newPassword);
				 mallAdministratorService.updatePersonInfo(curUser);
				 infoUpdateMessage="update person info sucessful";
				 }
				 model.addAttribute("message", infoUpdateMessage);
				return "module/personInfo";        	
			}
			

	 
	 /*
	  *Manage Advertisement
		Top 10 Products 
		Top 5 Stores
	  */
	 
	 /*
	  * Backup Database - Daily
	  */
	 
	 
	 /*
	  * View Sales History (Daily /Weekly/ Monthly/ Yearly) 
	  */
	 
	 /*
	  * Calculate PARKnSHOP.com Income from Sales Commission (2%)
		Daily /Weekly/ Monthly/ Yearly
	  */
	 
	 /*
	  * Manage Commission(管理授权)
		Modify Commission Rates
	  */
	 
	 /*
	  * Search Order by OrderID
	  */
	 
	 
	 
	 
	 
}